import com.dmotiko.general.*
import com.dmotiko.chabeli.*
import com.dmotiko.util.*
import mx.transitions.easing.Regular;
import mx.transitions.Tween;
import mx.utils.Delegate

class ChabeliNavBar
extends WebSiteNavBar {
	
	private var mcNosotros:ChabeliNavButton;
	private var mcProductos:ChabeliNavButton;
	private var mcContacto:ChabeliNavButton;
	private var mcActiveBtn:ChabeliNavButton;
	
	private var interpolate:Function;
	private var interpolator:MovieClip;
	
	private function ChabeliNavBar() {
		super("navBarButton");
		trace("new ChabeliNavBar ");
		var aBtns:Array = new Array();
		aBtns.push( { name: "mcNosotros", link: "nosotrosButton", section: ChabeliSite.NOSOTROS} );
		aBtns.push( { name: "mcProductos", link: "productosButton", section: ChabeliSite.PRODUCTOS} );
		aBtns.push( { name: "mcContacto", link: "contactoButton", section: ChabeliSite.CONTACTO} );
		this.setData( aBtns );
		this._y = 160;
		this._x = -300;
		/*
		MovieEffects.interpolate();
		this.interpolator.stopEffect();
		this.interpolator = this.interpolate( { prop: "_x", init: this._x, end: 0, ratio: 4 } );
		*/
		registerTween( "moveX", new Tween( this, "_x", Regular.easeOut, this._x, 0, 0.5, true) );
		ChabeliSite.getApp().addEventListener( ChabeliSite.SECTION_CHANGED, Delegate.create( this, sectionChanged) );
	}
	
	private function sectionChanged() {
		if (ChabeliSite.getApp().getSection() == ChabeliSite.HOME) {
			mcActiveBtn.setActive(false);
			mcActiveBtn.onRollOut();
			setActiveBtn(undefined);
		}
	}
		
	private function refreshData() {
		mapBtns = new Array();
		for(var i:Number = 0; i < data.length; i++){
			var btn:MovieClip = this.attachMovie(btnLinkage, data[i].name, this.getNextHighestDepth());
			btn.setData( data[i] );
			btn.attachMovie(data[i].link, "mcLabel", btn.mcLabel.getDepth(), { _x: 67, _y: -1 } );
			btn.mcSeparator._visible = i < data.length - 1;
			//btn.addEventListener( BaseClipEvent.READY, Delegate.create(this, childReady) );
			btn.addEventListener( WebSiteNavButton.RELEASE, Delegate.create(this, childChanged) );
			mapBtns.push( [ btn, data[i] ]);
			
		}
		layoutButtons();
	}
	
	private function childLoaded(evnt):Void {
		super.childReady(evnt);
	}
	
	private function layoutButtons() {
		var nY:Number = 0;
		for(var i:Number = 0; i < mapBtns.length; i++){
			var btn:MovieClip = mapBtns[i][0];
			btn._y = nY;
			nY += 30;
		}
	}
	
	private function childChanged(evnt):Void {
		if(this.getActiveBtn() != evnt.target){
			mcActiveBtn.setActive(false);
			mcActiveBtn.onRollOut();
			this.setActiveBtn(evnt.target);
		}
		mcActiveBtn.setActive(true);
		if(ChabeliSite.getApp().getSection() != evnt.target.data.section){
			ChabeliSite.getApp().setSection( evnt.target.data.section );
		}
		
	}
	
}